//
//  MovieViewController.m
//  product_B
//
//  Created by lanou on 16/5/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieMainModel.h"
#import "MovieMainTableViewCell.h"
#import "MovieDetailViewController.h"

@interface MovieViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSString *lastId;

@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"电影";
    self.navigationItem.title = @"电影";
//    self.musicButton.hidden = NO;
    self.dataArray = [NSMutableArray array];
    [self netRequest];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-113) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MovieMainTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.lastId = @"0";
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        if (self.dataArray.count == 0) {
            self.lastId = @"0";
        }else{
        MovieMainModel *model = self.dataArray[self.dataArray.count-1];
        self.lastId = model.myId;
        }
        [self netRequest];
    }];
}

- (void)netRequest{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/movie/list/%@",self.lastId] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArray = dataDic[@"data"];
        for (NSDictionary *dic in dataArray) {
            MovieMainModel *model = [[MovieMainModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self performSelector:@selector(endRefresh) withObject:self afterDelay:1];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)endRefresh{
    [self.tableView.mj_footer endRefreshing];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MovieMainModel *model = self.dataArray[indexPath.row];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    cell.scoreLabel.text = model.score;
    [cell.scoreLabel setFont:[UIFont fontWithName:@"Marker Felt" size:25]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc]init];
    movieDetailVC.movieMainModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:movieDetailVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
